package com.google.zxing.client.android;

import java.util.ArrayList;

import com.google.zxing.oned.OneDReader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

public class layout extends Activity implements SeekBar.OnSeekBarChangeListener {
	String userName;
	String passwd;
	// String userNameNew,passwordNew;
	EditText username, password, newUsername, newPassword;
	TableLayout register;
	TableLayout signin;
	TableLayout intro;
	ScrollView filter;
	ArrayList<String> users;
	ArrayList<String> passwords;
	TextView choices;
	ArrayList<String> names;
	Context ctx;
	// ProgressDialog myProgressDialog;

	ProgressDialog myProgressDialog = null;

	private Handler handler = new Handler();

	SeekBar mSeekBar;
	TextView mProgressText;
	double radius;
	Thread imageThread;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		
		setContentView(R.layout.main);
		ctx = this.getApplicationContext();
		
		Bundle bundle1 = getIntent().getExtras();
		if (bundle1 != null) {
			String activity = bundle1.getString("Activity");
			if (activity.equals("Main")) {
				showHomePage();
			}
		} else
			
		{
			intro = (TableLayout) findViewById(R.id.intro);
			intro.setVisibility(View.VISIBLE);
			imageThread = new Thread(threadsleep);
			imageThread.start();
		}
			//showSigninPage();
			

		//mainProcessing();

	

		// runOnUiThread(imageThread);

		username = (EditText) findViewById(R.id.editName);
		password = (EditText) findViewById(R.id.editPasswd);
		newUsername = (EditText) findViewById(R.id.neweditName);
		newPassword = (EditText) findViewById(R.id.neweditPasswd);
		users = new ArrayList<String>();
		passwords = new ArrayList<String>();
		choices = (TextView) findViewById(R.id.text);
		names = new ArrayList<String>();

		// userName=username.getEditableText().toString();
		// passwd=password.getEditableText().toString();

		/*** Seek Bar for Radius ***/
		mSeekBar = (SeekBar) findViewById(R.id.seek);
		mSeekBar.setOnSeekBarChangeListener(this);
		mProgressText = (TextView) findViewById(R.id.progress);

		Button button = (Button) findViewById(R.id.buttonSignIn);
		Button visitor = (Button) findViewById(R.id.visitor);
		OnClickListener signin = new Button.OnClickListener() {
			// @Overrides
			public void onClick(View v) {
				userName = username.getText().toString();
				passwd = password.getText().toString();
				if (chkUser(userName, passwd)) {
					Log.e("*******", "started app");
					signinPageGone();
					showHomePage();
					/*
					 * Intent i = new Intent(getApplicationContext(),
					 * Main.class); onStop(); startActivity(i);
					 */
				}
			}
		};
		button.setOnClickListener(signin);
		visitor.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {
				signinPageGone();
				showHomePage();

			}

		});
		Button signup = (Button) findViewById(R.id.buttonSignUp);
		signup.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				showSignupPage();
				signinPageGone();

			}
		});

		Button registering = (Button) findViewById(R.id.buttonRegister);
		registering.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				// userName=newUsername.getText().toString();
				// passwd=newPassword.getText().toString();
				users.add(newUsername.getText().toString());
				passwords.add(newPassword.getText().toString());
				username.setText(newUsername.getText().toString());
				password.setText(newPassword.getText().toString());
				registerPageGone();
				showSigninPage();
				showHomePage();

			}
		});
		Button submitFilter = (Button) findViewById(R.id.submitFilter);
		submitFilter.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				homePageGone();
				Intent i = new Intent(ctx, Main.class);
				Bundle bundle1 = new Bundle();
				i.putExtra("Activity", "layout");
				i.putStringArrayListExtra("FeatureCodes", names);
				Log.e("Layout","Radius is: "+radius);
				i.putExtra("radius", radius);
				setResult(RESULT_OK, i);
				startActivity(i);
				finish();

			}
		});
	}

	private Runnable threadsleep = new Runnable() {

		public void run() {

			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// imageThread.stop();
			// showSigninPage();
			handler.post(signinRunnable);
		}

	};
	private Runnable signinRunnable = new Runnable() {

		public void run() {

			showSigninPage();

		}

	};

	@Override
	public void onStop() {
		super.onStop();
		finish();
	}

	public boolean chkUser(String username, String password) {

		users.add("a");

		passwords.add("a");
		for (int i = 0; i < users.size(); i++) {

			if ((username.equals(users.get(i)))
					&& (password.equals(passwords.get(i)))) {

				Log.e("*****", "Correct user");
				Toast.makeText(layout.this, "This is a correct user",
						Toast.LENGTH_SHORT).show();
				return true;
			} else {
				Log.e("**username", username);
				Log.e("**password", password);
				Log.e("*****", "wrong user");
				Toast
						.makeText(
								ctx,
								"The user name or password you enetered is not true, please try again",
								Toast.LENGTH_LONG).show();

				return false;
			}
		}
		return false;
	}

	public void showHomePage() {
		// filter = (TableLayout) findViewById(R.id.filter_layout);
		filter = (ScrollView) findViewById(R.id.filter_layout);
		filter.setVisibility(View.VISIBLE);
		filterHandling();
	}

	public void showSignupPage() {
		register = (TableLayout) findViewById(R.id.registerLayout);
		register.setVisibility(View.VISIBLE);
	}

	public void showSigninPage() {
		signin = (TableLayout) findViewById(R.id.signinLayout);
		intro = (TableLayout) findViewById(R.id.intro);
		intro.setVisibility(View.GONE);
		signin.setVisibility(View.VISIBLE);
	}

	public void signinPageGone() {
		signin = (TableLayout) findViewById(R.id.signinLayout);
		signin.setVisibility(View.GONE);
	}

	public void registerPageGone() {
		register = (TableLayout) findViewById(R.id.registerLayout);
		register.setVisibility(View.GONE);
	}

	public void homePageGone() {
		// filter = (TableLayout) findViewById(R.id.filter_layout);
		filter = (ScrollView) findViewById(R.id.filter_layout);
		filter.setVisibility(View.GONE);
	}

	public void filterHandling() {
		CheckBox bldgs = (CheckBox) findViewById(R.id.chk_Bldgs);
		CheckBox cafes = (CheckBox) findViewById(R.id.chk_cafes);
		CheckBox rooms = (CheckBox) findViewById(R.id.chk_rooms);
		CheckBox office = (CheckBox) findViewById(R.id.chk_offices);
		CheckBox sports = (CheckBox) findViewById(R.id.sport);
		CheckBox park = (CheckBox) findViewById(R.id.park);
		CheckBox lab = (CheckBox) findViewById(R.id.lab);
		// ImageButton building= (ImageButton)findViewById(R.id.build);

		OnClickListener check_listener = new OnClickListener() {
			public void onClick(View v) {
				final CheckBox cb = (CheckBox) v;
				if (cb.isChecked()) {

					updateChoiceString(cb.getText().toString(), true);
				} else {
					updateChoiceString(cb.getText().toString(), false);
				}
			}
		};
		bldgs.setOnClickListener(check_listener);
		cafes.setOnClickListener(check_listener);
		rooms.setOnClickListener(check_listener);
		office.setOnClickListener(check_listener);
		sports.setOnClickListener(check_listener);
		park.setOnClickListener(check_listener);
		lab.setOnClickListener(check_listener);
		// building.setOnClickListener(check_listener);
	}

	public void updateChoiceString(String checkBoxname, boolean add) {
		choices.setVisibility(View.VISIBLE);
		choices.setText("you chose to see: " + "\n");

		if (add) {

			names.add(checkBoxname);
		} else {

			names.remove(checkBoxname);
		}
		for (int i = 0; i < names.size(); i++) {

			choices.append(names.get(i));
			if (i == names.size() - 1)
				choices.append(". ");
			else
				choices.append(", ");
		}

	}

	private void mainProcessing() {

		Thread thread = new Thread(null, doBackgroundProc, "Background2");

		thread.start();

		myProgressDialog = ProgressDialog.show(layout.this, "Please wait...",
				"Doing Extreme Calculations...", true);

	}

	private Runnable doBackgroundProc = new Runnable() {

		public void run() {

			backgroundProc();

		}

	};

	private void backgroundProc() {

		try {

			// Do some Fake-Work
			Log.e("Main", "in the working thread");

			Thread.sleep(2000);

		} catch (Exception e) {
			Log.e("Main", "in catch of backgroundProc");
		}

		myProgressDialog.dismiss();
	}

	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromTouch) {

		//progress= Integer.parseDouble(progress);
		double maram= (double)(2/1000);
		double ghada= (2.0/1000);
		Log.e("layout","ghada: "+ghada+" "+ maram);
		radius = progress / 1000;
		Log.e("Layout","Radius is after calc.: "+radius);
		String unit;
		if (progress > 999) {
			unit = "Km";
			progress = progress / 1000;
		} else
			unit = "m";

		mProgressText.setText("Radius= " + progress + " " + unit);

	}

	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

}
